<?php
namespace Admin\Controller;

/**
 * 后台首页控制器
 */
class WxController extends ThinkController {

    protected function _wx() {
        $wx = C('WX');
        $token = $wx['token'];
        get_token($token);
        //$info = get_user_list($token);
        $dao = M('WxFollow');
        $maxkey=$dao->field('max(`id`) maxid')->find();
        $sql="INSERT IGNORE  iic_cn_web.`iic_wx_follow` (`id`,`token`,  `openid`,  `subscribe_time`,  `language`) ";
        $sql.="SELECT `id`,`token`,  `openid`,  `subscribe_time`,  `language` FROM iic_cn_wp.`wp_follow` WHERE `id` > {$maxkey['maxid']};";
        M()->execute($sql);
        // dump($dao->getLastSql());
        // if ($info['count'] > 0) {
        // $i=0;
        // foreach ($info['data']['openid'] as $k => $vo) {
        // if($dao->getByOpenid($vo)){
        // continue;
        // }
        // $arr = array();
        // $arr['openid'] = $vo;
        // $arr['token'] =$token;
        // $i=$i++;
        // if($i<10){
        // $uinfo = getWeixinUserInfo($vo,$token);
        // $arr['subscribe'] = $uinfo['subscribe'];
        // $arr['nickname'] = $uinfo['nickname'];
        // $arr['sex'] = $uinfo['sex'];
        // $arr['language'] = $uinfo['language'];
        // $arr['city'] = $uinfo['city'];
        // $arr['province'] = $uinfo['province'];
        // $arr['country'] = $uinfo['country'];
        // $arr['headimgurl'] = $uinfo['headimgurl'];
        // $arr['subscribe_time'] = $uinfo['subscribe_time'];
        // $arr['unionid'] = $uinfo['unionid'];
        // $arr['remark'] = $uinfo['remark'];
        // $arr['groupid'] = $uinfo['groupid'];
        // }
        // $arr['mTime'] = 0;
        // $dao -> add($arr);
        // }
        // }else{
        $list = $dao -> where(array('unionid' => '','subscribe'=>'-1')) -> limit('0,50') -> order('id DESC') -> select();
        // dump($dao->getlastsql());
        $access_token = get_access_token ( $token );
        foreach ($list as $arr) {
            $uinfo = getWeixinUserInfo($arr['openid'], $arr['token'],$access_token);
            $arr['subscribe'] = $uinfo['subscribe'];
            if($uinfo['subscribe']!=0){
                $arr['nickname'] = $uinfo['nickname'];
                $arr['sex'] = $uinfo['sex'];
                $arr['language'] = $uinfo['language'];
                $arr['city'] = $uinfo['city'];
                $arr['province'] = $uinfo['province'];
                $arr['country'] = $uinfo['country'];
                $arr['headimgurl'] = $uinfo['headimgurl'];
                $arr['subscribe_time'] = $uinfo['subscribe_time'];
                $arr['unionid'] = $uinfo['unionid'];
                $arr['remark'] = $uinfo['remark'];
                $arr['groupid'] = $uinfo['groupid'];
                $arr['mTime'] = 0;
            }
            $dao -> where(array('openid' => $arr['openid'])) -> save($arr);
            //}
        }
    }

    /**
     * 后台首页
     */
    public function index($p = 0) {
        $this -> _wx();
        $this -> meta_title = '微信用户首页';
        parent::lists('wx_follow', $p);
    }

}
